package org.learn.java.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Test;
import org.learn.java.annotation.MyAnnotation;

public class MyReflectTest {
	private static Method[] methods = null;
	private static Method[] primethods = null;
	private static Class cl = MyReflect.class;

	/**
	 * @param args
	 */
	public static void test(String[] args) {
		methods = getMethods(cl, 1);
		printMethodName(methods);
	}

	/**
	 * Class
	 * 
	 * @return
	 */
	//@Test
	public void classInfo() {
		// 获取Class方式
		String path = "org.learn.java.reflect.MyReflect";
		Class cl = null;
		try {
			// 1
			cl = Class.forName(path);
		} catch (ClassNotFoundException e) {

			e.printStackTrace();
		}
		// 2
		cl = MyReflect.class;
		// 3
		cl = new MyReflect().getClass();

		// 获取类名
		String className = cl.getName();
		pr(className);
		String simpleName = cl.getSimpleName();
		pr(simpleName);

		// 获取修饰符
		int modifiers = cl.getModifiers();
		pr(modifiers);

		// 获取包信息
		Package packages = cl.getPackage();
		pr(packages);

		// 获取父类
		Class sup = cl.getSuperclass();
		pr(sup.getName());

		// 获取实现的接口
		Class[] interfaces = cl.getInterfaces();
		for (Class class1 : interfaces) {
			pr(class1.getName());
		}

		// 获取构造器
		Constructor[] constructors = cl.getConstructors();
		for (Constructor constructor : constructors) {
			pr(constructor.getName());
		}

		// 获取方法
		Method[] methods = cl.getMethods();

		// 获取属性
		Field[] fields = cl.getFields();

		// 获取注解
		Annotation[] annotations = cl.getAnnotations();

		// 获取实例对象
		try {
			MyReflect myReflect = (MyReflect) cl.newInstance();
			myReflect.say();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void constroctors() {
		// 获取构造器
		Constructor[] constructors = cl.getConstructors();
		for (Constructor constructor : constructors) {
			pr(constructor.getName());
		}
		Constructor cs = null;
		// 获取指定构造器
		try {
			cs = cl.getConstructor(Long.class);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 获取构造方法参数信息
		Class[] parameterTypes = cs.getParameterTypes();

		// 实例化对象
		try {
			MyReflect myReflect = (MyReflect) cs.newInstance(Long.valueOf("3"));
			myReflect.say();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 运行时获取方法
	 * 
	 * @param cl
	 * @param type
	 * @return
	 */
	public static Method[] getMethods(Class cl, int type) {
		Method[] methods = null;
		if (1 == type) {
			methods = cl.getMethods(); // 获取全部方法
		} else {
			methods = cl.getDeclaredMethods(); // 获取自定义方法
		}
		return methods;
	}

	public static void paramAnotation(Method method) {
		Annotation[][] annotations = method.getParameterAnnotations();

	}

	public static void printMethodName(Method[] methods) {
		for (Method method : methods) {
			System.out.println("methodName:" + method.getName());
		}
	}

	public static Class getMyClassByPath(String path) {
		Class<?> clzz = null;
		try {
			clzz = Class.forName(path);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return clzz;
	}

	public static void getConstroctors(Class<?> clazz) {
		Constructor<?>[] constructors = clazz.getConstructors();
		Constructor constructor = constructors[0];
	}

	public static void pr(Object obj) {
		System.out.println(obj);
	}
}
